package com.mayblackcat.common.vaild;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName : ListValueConstraintValidator
 * @Description : 实现只含有0,1数字的校验，作为校验器
 * @Author : MayBlackCat
 * @Date: 2021-08-27 10:54
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {
    private Set<Integer> set;

    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] values = constraintAnnotation.values();
        //懒加载机制
        set=new HashSet<>();
        //将非重复数字添加到set中进行校验
        for (int v:values){
            set.add(v);
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}
